package com.ltu.service.mqtt;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ltu.biz.MqOrderServiceBize;
import com.ltu.enums.EnumUtils;
import com.ltu.enums.MqTopiceDict;
import com.ltu.util.SpringContextHolder;
import com.ltu.util.SpringUtil;

import lombok.extern.slf4j.Slf4j;


/**
 * @Classname PushCallback
 * @Description 消费监听类
 */
@Slf4j
@Component
public class MqttListenterTopic implements MqttCallback {
	@Autowired
	private MqOrderServiceBize mqOrderServiceBize;

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        log.info("接收消息主题 : " + topic);
//        log.info("接收消息Qos : " + message.getQos());
        String msg = new String(message.getPayload());
        log.info("收到的消息内容："+msg);
        if(topic.indexOf("$SYS/brokers") >= 0) {
        	int index =  topic.lastIndexOf("/");
        	topic =  topic.substring(index+1);
        }
        	   MqTopiceDict.OrderEnum  dict  =   EnumUtils.strToEnum(MqTopiceDict.OrderEnum.class, topic);
        	   if(dict == null) {
        		   log.info("\n 无效的topic: {}",topic);
        		   return;
        	   }
               switch (dict) {
        			case OrderPayListen:
        				mqOrderServiceBize.rollBackOrder(msg);
        				break;
        			default:
        				break;
        	}
        
        
     
     
        
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("deliveryComplete---------" + token.isComplete());
    }

	@Override
	public void connectionLost(Throwable cause) {
		System.out.println("连接出错了");
		cause.printStackTrace();
		
	}
}